package com.autonavi.yunda.yunji.core.utils;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.common.utils.clock.AmapClock;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cizhong.wcs
 * @date 2022/2/28 11:48 上午
 */
@Slf4j
public class ValidUtil {
    public static Map<String, Method> nameMethodMap = new HashMap<>();

    static {
        for (Method method : ValidUtil.class.getMethods()) {
            nameMethodMap.put(method.getName(), method);
        }
    }

    private ValidUtil() {
    }

    /**
     * @param key   参数名称
     * @param value 参数值
     */
    public static void notNull(String key, Object value) {
        if (value == null) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, key + "不能为null");
        }
    }

    /**
     * @param key   参数名称
     * @param value 参数值
     * @param min   参数最小值
     */
    public static boolean min(String key, Long value, Long min) {
        if (value.compareTo(min) < 0) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, key + "不能小于" + min + " value = " + value);
        }
        return true;
    }

    /**
     * @param key   参数名称
     * @param value 参数值
     */
    public static boolean notBlank(String key, String value) {
        if (Strings.isBlank(value)) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, key + "不能为空");
        }
        return true;
    }

    public static void notBlank(String key, Object value) {
        if (value == null || Strings.isBlank(value.toString())) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, key + "不能为空");
        }
    }

    public static boolean checkExpressionAndThrow(boolean expression, String message) {
        if (!expression) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, message);
        }
        return true;
    }

    public static boolean checkExpressionAndReturn(boolean expression, String message) {
        log.warn("expression = {},message = {}", expression, message);
        return expression;
    }

    /**
     * 验证月份是否合理, 介于200001 到 当前月份
     *
     * @param key   参数名称
     * @param value 参数值
     */
    public static boolean checkMonth(String key, Integer value) {
        int minMonth = 201801;
        int maxMonth = Integer.parseInt(AmapClock.dateTimeStringFromTimestamp(AmapClock.now(), AmapClock.MONTH_FORMAT));

        if (value < minMonth || value > maxMonth) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, key + " 必须介于200001 和 " + maxMonth + "之间,value = " + value);
        }
        return true;
    }

    /**
     * @param key   参数名称
     * @param value 参数值
     * @param regex 正则表达式
     */
    public static boolean checkRegex(String key, Object value, String regex) {
        String valueStr = value.toString();
        Matcher matcher = Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(valueStr);
        if (!matcher.matches()) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, key + " 正则不匹配, value = " + value + ",regex = " + regex);
        }
        return true;
    }

    public static boolean checkRegexAndReturn(String value, String regex) {
        Matcher matcher = Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(value);
        return matcher.matches();
    }

}
